home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Medal Software 3
/
Gold Medal Software - Volume 3 (Gold Medal) (1994).iso
/
print
/
bjfilt.arj
/
FINDSTR.C
< prev
next >
Wrap
Text File
|
1994-02-03
|
1KB
|
47 lines
/* findstr( char * str1, int n1, char *str2, int n2)
* find if str1 overlaps str2 - ie if it is contained in str2, or if string 1 overlaps the end of
* On error ( strings of negative length) or no match, returns -1,
*otherwise return the length into string 2 that the match (or partial match) occurs.
*Copyright (C) W. G. Unruh Jan 29 1994
*/
int findstr( char * str1, int n1, char *str2, int n2)
{
char c;
int i,j,k;
c = str1[0];
if(n1<=0 || n2<=0) return(-1);
if(n2>n1)
{
for(i=0;i<n2-n1+1;i++)
{
if( c == str2[i]) /* First character matches, check the rest*/
{ for(j=1;j<n1;j++)
{ if ( str2[i+j]!=str1[j]) break;
}
if(j==n1) return (i);
}
/* Have we found a match for all of them?*/
}
}
/* No complete matches- Now check the end of the string */
/* End of String */
k=(n2>n1?n2:n1-1);
{
for (i=k-n1+1 ; i<n2;i++)
{
if(c==str2[i])
{
for(j=1;j<n2-i;j++)
{ if(str1[j]!= str2[i+j]) break;
}
if (j==n2-i) return (i);
}
}
}
return(-1);
}